#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
	int val;
	if (argc != 2) {
		printf("usage: 3-4 fd\n");
		return -1;
	}

	if ((val=fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {
		printf("fcntl error: %s\n", strerror(errno));
		return -1;
	}

	switch (val & O_ACCMODE) {
		case O_RDONLY:
			printf("readonly");
			break;	
		case O_WRONLY:
			printf("writeonly");
			break;	
		case O_RDWR:
			printf("readwrite");
			break;	
		default:
			printf("unknown");
			break;
	}

	if (val & O_APPEND)
		printf(", append");
	if (val & O_NONBLOCK)
		printf(", nonbloking");
	if (val & O_SYNC)
		printf(", syncwrite");
	
	printf("\n");
	return 0;
}